package br.ufrj.dcc.dojo.jtrace.interceptor;

import org.jtrace.Hit;
import org.jtrace.Jay;
import org.jtrace.Scene;
import org.jtrace.Tracer;
import org.jtrace.TracerInterceptor;
import org.jtrace.geometry.GeometricObject;
import org.jtrace.lights.Light;
import org.jtrace.primitives.ColorRGB;
import org.jtrace.primitives.Point3D;
import org.jtrace.primitives.Vector3D;
import org.jtrace.shader.Shader;

public class ShadowInterceptor implements TracerInterceptor {

    Tracer tracer;
    Scene scene;

    @Override
    public void init(Tracer tracer, Scene scene) {
        this.tracer = tracer;
        this.scene = scene;
    }

    @Override
    public void beforeShade(Light light, ColorRGB color) {
        // TODO Auto-generated method stub
    }

    @Override
    public boolean shouldShade(Shader shader, Light light, Hit hit, Jay jay, GeometricObject object) {
    	
        Point3D point = hit.getPoint(jay);

        Vector3D direction = new Vector3D(point, light.getPosition());
        Jay ray = new Jay(point, direction.normalize());

        Hit hitRay = tracer.cast(scene, ray);

        if (hitRay.isHit()) {
            if (hitRay.getT() < direction.module()) {
                return false;
            }
        }

        return true;
    }

    @Override
    public void afterShade(Light light, ColorRGB color) {
        // TODO Auto-generated method stub
    }

}